package coms.fourthirty.emm;

import coms.fourthirty.emm.resources.EMSResources;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EMSNewMessage extends Activity {
	String username;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle grundle) {
		super.onCreate(grundle);
		setContentView(R.layout.new_message);

		Bundle extras = getIntent().getExtras();

		// username = extras.getString("coms.fourthirty.emm.username");
		username = EMSResources.username;
		String to, subject;
		to = extras.getString("coms.fourthirty.emm.to");
		subject = extras.getString("coms.fourthirty.emm.subject");

		if (to != null)
			((EditText) findViewById(R.id.edit_text_new_message_to))
					.setText(to);
		if (subject != null)
			((EditText) findViewById(R.id.edit_text_new_message_subject))
					.setText(subject);
	}

	public void send(View view) {
		String to, subject, body;
		to = ((EditText) findViewById(R.id.edit_text_new_message_to)).getText()
				.toString();
		subject = ((EditText) findViewById(R.id.edit_text_new_message_subject))
				.getText().toString();
		body = ((EditText) findViewById(R.id.edit_text_new_message_body))
				.getText().toString();

		makeAndSendMessage(to, username, subject, body);
	}

	public void makeAndSendMessage(String to, String from, String subject,
			String body) {
		if (!to.equals("System") && !to.equals("")) {
			// TODO: send stuff to server
			// return to Message List view so the user can view more messages.
			Intent i = new Intent(EMSNewMessage.this, EMSMessages.class);
			i.putExtra("messageSent", true);
			// i.putExtra("coms.fourthirty.emm.username", username);
			startActivity(i);
		}
		else
			Toast.makeText(getBaseContext(), "Invalid recipient", Toast.LENGTH_LONG).show();
	}
}